package com.derongonline.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.derongonline.R;
import com.derongonline.adapter.GuideAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements View.OnClickListener {

    private ViewPager pager;
    private GuideAdapter adpater;
    private List<View> views;
    private Button startBtn;
    private static final int[] pics = {R.layout.guid_view_1,
            R.layout.guid_view_2, R.layout.guid_view_3, R.layout.guid_view_4};
    private ImageView[] dots;
    private int currentPosition;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        views = new ArrayList<>();
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);
            if (i == pics.length - 1) {
                startBtn = (Button) view.findViewById(R.id.enter);
                startBtn.setTag("enter");
                startBtn.setOnClickListener(this);
            }
            views.add(view);
        }
        pager = (ViewPager) findViewById(R.id.guide);
        adpater = new GuideAdapter(views);
        pager.setAdapter(adpater);
        pager.setOnPageChangeListener(new PageChangeListener());
        initDots();
    }

    @Override
    public void onClick(View view) {
        if (view.getTag().equals("enter")) {
            main();
            return;
        }
        int position = (Integer) view.getTag();
        setCurrentView(position);
        setCurrentDot(position);
    }

    private void main() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void initDots() {
        LinearLayout layout = (LinearLayout) findViewById(R.id.dots);
        dots = new ImageView[pics.length];
        for (int i = 0; i < pics.length; i++) {
            dots[i] = (ImageView) layout.getChildAt(i);
            dots[i].setEnabled(false);
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);
        }
        currentPosition = 0;
        dots[currentPosition].setEnabled(true);
    }


    private void setCurrentView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        pager.setCurrentItem(position);
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > pics.length || currentPosition == position) {
            return;
        }
        dots[position].setEnabled(true);
        dots[currentPosition].setEnabled(false);
        currentPosition = position;
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setCurrentDot(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}
